package com.dzm.portal.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.system.api.domain.BrandEntity;
import com.xpand.starter.canal.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 作者：Dzm
 * 时间：2024-09-11-14:35
 * 功能：
 */

@CanalEventListener
public class CanalEventDataListener {

    @Autowired
    private RedisService redisService;

    /**
     * 监听brand表中数据的添加数据的添加
     * @param eventType
     * @param rowData
     */
    @InsertListenPoint(table = "tb_brand")
    public void onEventInsert(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        System.out.println("添加操作：");
        BrandEntity brandEntity = new BrandEntity();
        //读取正在操作后的数据
        for (CanalEntry.Column column : rowData.getAfterColumnsList()) {
            String columnName = column.getName();
            String columnValue = column.getValue();

            if("id".equals(columnName)) {
                brandEntity.setId(Long.parseLong(columnValue));
            }else if("name".equals(column.getName())){
                brandEntity.setName(columnValue);
            }else if("first_char".equals(columnName)) {
                brandEntity.setFirstChar(columnValue);
            }

        }
        //放入缓存
        redisService.setCacheMapValue(CacheConstants.BRAND_LIST, brandEntity.getId().toString(), brandEntity);
    }

    /**
     * 监听brand表中数据的添加数据的修改
     * @param eventType
     * @param rowData
     */
    @UpdateListenPoint(table = "tb_brand")
    public void onEventUpdate(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        System.out.println("修改操作：");
        BrandEntity brandEntity = new BrandEntity();
        //读取正在操作后的数据
        for (CanalEntry.Column column : rowData.getAfterColumnsList()) {
            String columnName = column.getName();
            String columnValue = column.getValue();

            if("id".equals(columnName)) {
                brandEntity.setId(Long.parseLong(columnValue));
            }else if("name".equals(column.getName())){
                brandEntity.setName(columnValue);
            }else if("first_char".equals(columnName)) {
                brandEntity.setFirstChar(columnValue);
            }

        }
        //放入缓存
        redisService.setCacheMapValue(CacheConstants.BRAND_LIST, brandEntity.getId().toString(), brandEntity);
    }

    /**
     * 监听brand表中数据的添加数据的删除
     * @param eventType
     * @param rowData
     */
    @DeleteListenPoint(table = "tb_brand")
    public void onEventDelete(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        System.out.println("删除操作：");
        //读取正在操作前的数据
        for (CanalEntry.Column column : rowData.getBeforeColumnsList()) {
            String columnName = column.getName();
            String columnValue = column.getValue();

            if("id".equals(columnName)) {
                //删除对应缓存
                redisService.deleteCacheMapValue(CacheConstants.BRAND_LIST, columnValue);
            }

        }
    }

    /**
     * 监听brand表中数据的多个操作
     * @param eventType
     * @param rowData
     */
    @ListenPoint(destination = "example", schema = "dxgdb", table = "tb_brand",
            eventType = {
            CanalEntry.EventType.INSERT,
            CanalEntry.EventType.UPDATE,
            CanalEntry.EventType.DELETE
            })
    public void onEvent(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        System.out.println("出现操作事件：");
        System.out.println("操作之前：");
        rowData.getBeforeColumnsList().forEach(c -> {
            System.out.println("Name" + c.getName() + "-----" + "Value = " + c.getValue());
        });
        System.out.println("操作之后：");
        rowData.getAfterColumnsList().forEach(c -> {
            System.out.println("Name" + c.getName() + "-----" + "Value = " + c.getValue());
        });
    }
}
